// Register LabelCloud
BloggerExt.labelcloud = function() {

    var arLabels = new Array();
    var arLabelCounts = new Array();
    var labelMaxCount = 0;

    // by delicious
    function s(a,b,i,x){
        if(a>b){ var m=(a-b)/Math.log(x),v=a-Math.floor(Math.log(i)*m); }
        else{ var m=(b-a)/Math.log(x),v=Math.floor(Math.log(i)*m+a); }
        return v;
    }

    jQuery('#'+BloggerExtOptions.labelcloud_widget_id+' div li').each(function(idx){
        var txt = jQuery(this).text();
        var i = txt.indexOf('(');
        var j = txt.indexOf(')');
        var labelCount = txt.substr(i+1, (j-i-1));
        arLabels[idx] = this;
        arLabelCounts[idx] = labelCount;
        if(parseInt(labelCount) > labelMaxCount) labelMaxCount = parseInt(labelCount);
    });
    for (var i=0; i<arLabels.length; i++) {
        var liObj = jQuery(arLabels[i]);
        var aObj = liObj.find('a');
        var v = s(BloggerExtOptions.labelcloud_minFontSize, BloggerExtOptions.labelcloud_maxFontSize, arLabelCounts[i], labelMaxCount);
        var r = s(BloggerExtOptions.labecloud_minColor[0], BloggerExtOptions.labelcloud_maxColor[0], arLabelCounts[i], labelMaxCount);
        var g = s(BloggerExtOptions.labecloud_minColor[1], BloggerExtOptions.labelcloud_maxColor[1], arLabelCounts[i], labelMaxCount);
        var b = s(BloggerExtOptions.labecloud_minColor[2], BloggerExtOptions.labelcloud_maxColor[2], arLabelCounts[i], labelMaxCount);
        if(aObj.length == 1) {
            aObj.css({'font-size': v, 'color': 'RGB('+r+','+g+','+b+')'});
            if (BloggerExtOptions.labelcloud_display_count) aObj.html(aObj.html() + ' (' + arLabelCounts[i]  + ')');
            liObj.html(aObj);
        }else {
            liObj.css({'font-size': v, 'color': 'RGB('+r+','+g+','+b+')'});
        }
        liObj.css({'display': 'inline'});
    }
};
BloggerExt.register('labelcloud' ,  {
    labelcloud: true,
    labelcloud_version: 0.2,
    labelcloud_widget_id: 'Label1',
    labelcloud_maxFontSize: 24,
    labelcloud_minFontSize: 10,
    labelcloud_maxColor: [168,181,248],
    labecloud_minColor: [0,0,0],
    labelcloud_display_count: false
});

